package cn.wolfcode.controller;

import cn.wolfcode.domain.User;
import cn.wolfcode.redis.service.IUserRedisService;
import cn.wolfcode.service.IUserService;
import cn.wolfcode.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserRedisService userRedisService;

    @PostMapping("/login")
    public Object login(String username, String password) {
        User user = userService.login(username, password);
        String token = userRedisService.setToken(user);

        if (user == null){
            return JsonResult.error(JsonResult.CODE_ERROR,"登录失败",null);
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",user);
        return JsonResult.success(map);
    }

}
